package com.xjrsoft.module.customer.erp_model.erpPurchaseOrderInfo.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xjrsoft.common.page.PageOutput;
import com.xjrsoft.common.page.ConventPage;
import java.util.List;
import com.xjrsoft.common.utils.DataTransUtil;
import com.xjrsoft.common.result.Response;
import com.xjrsoft.core.tool.utils.BeanUtil;
import com.xjrsoft.module.base.service.IXjrBaseCodeRuleService;
import com.xjrsoft.module.customer.erp_model.erpPurchaseOrderInfo.dto.ErpPurchaseorderDto;
import com.xjrsoft.module.customer.erp_model.erpPurchaseOrderInfo.dto.SaveErpPurchaseorderFormDataDto;
import com.xjrsoft.module.customer.erp_model.erpPurchaseOrderInfo.vo.ErpPurchaseorderFormDataVo;
import com.xjrsoft.module.customer.erp_model.erpPurchaseOrderInfo.vo.ErpPurchaseorderListVo;
import com.xjrsoft.module.customer.erp_model.erpPurchaseOrderInfo.dto.ErpPurchaseorderListDto;
import com.xjrsoft.module.customer.erp_model.erpPurchaseOrderInfo.vo.ErpPurchaseorderentryVo;
import com.xjrsoft.module.customer.erp_model.erpPurchaseOrderInfo.entity.ErpPurchaseorderentry;
import com.xjrsoft.module.customer.erp_model.erpPurchaseOrderInfo.dto.ErpPurchaseorderentryDto;

import java.util.Map;
import java.util.stream.Collectors;
import java.io.File;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.xjrsoft.core.tool.utils.CollectionUtil;
import com.xjrsoft.core.tool.utils.IoUtil;
import com.xjrsoft.module.excel.entity.XjrExcelImport;
import com.xjrsoft.module.excel.factory.ParserFactory;
import com.xjrsoft.module.excel.parser.ImportDataParser;
import com.xjrsoft.module.excel.service.IXjrExcelImportService;
import org.springframework.web.multipart.MultipartFile;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;

import com.xjrsoft.core.tool.utils.Func;
import org.springframework.web.bind.annotation.*;
import com.xjrsoft.module.customer.erp_model.erpPurchaseOrderInfo.entity.ErpPurchaseorder;
import com.xjrsoft.module.customer.erp_model.erpPurchaseOrderInfo.vo.ErpPurchaseorderVo;
import com.xjrsoft.module.customer.erp_model.erpPurchaseOrderInfo.service.IErpPurchaseorderService;

/**
 *  控制器
 *
 * @author pp
 * @since 2021-10-13
 */
@RestController
@AllArgsConstructor
@RequestMapping("/erp_purchaseorderinfo")
@Api(value = "采购订单", tags = "采购订单接口")
public class ErpPurchaseorderController {

	private final IXjrExcelImportService importService;

	private final ObjectMapper objectMapper;

	private final IErpPurchaseorderService erpPurchaseorderService;

	private final IXjrBaseCodeRuleService codeRuleService;

	/**
	 * 详情
	 */
	@GetMapping("/{id}")
	@ApiOperation(value = "详情", notes = "传入erpPurchaseorder")
	public Response<ErpPurchaseorderFormDataVo> getFormData(@PathVariable String id) {
		ErpPurchaseorderFormDataVo formDataVo = new ErpPurchaseorderFormDataVo();
		// 主表数据
		ErpPurchaseorder erpPurchaseorder = erpPurchaseorderService.getById(id);
		formDataVo.setErpPurchaseorderVo(BeanUtil.copy(erpPurchaseorder, ErpPurchaseorderVo.class));
		// 从表数据
		formDataVo.setErpPurchaseorderentryVoList(BeanUtil.copyList(erpPurchaseorderService.getErpPurchaseorderentryByParentId(id), ErpPurchaseorderentryVo.class));
		return Response.ok(formDataVo);
	}

	/**
	 * 自定义分页 
	 */
	@GetMapping
	@ApiOperation(value = "分页", notes = "传入erpPurchaseorder")
	public Response<PageOutput<ErpPurchaseorderListVo>> getPageList(ErpPurchaseorderListDto listDto) {
		IPage<ErpPurchaseorder> page = erpPurchaseorderService.getPageList(listDto);
		List<ErpPurchaseorderListVo> records = BeanUtil.copyList(page.getRecords(), ErpPurchaseorderListVo.class);
		// 转换列表数据
		DataTransUtil.transListShowData(records);
		// 工作流数据处理
		return Response.ok(ConventPage.getPageOutput(page.getTotal(), records));
	}

	/**
	 * 新增 
	 */
	@PostMapping
	@ApiOperation(value = "新增", notes = "传入erpPurchaseorder")
	public Response save(@RequestBody SaveErpPurchaseorderFormDataDto formDto) {
		ErpPurchaseorderDto erpPurchaseorderDto = formDto.getErpPurchaseorderDto();
		ErpPurchaseorder erpPurchaseorder = BeanUtil.copy(erpPurchaseorderDto, ErpPurchaseorder.class);
		List<ErpPurchaseorderentryDto> erpPurchaseorderentryDtoList = formDto.getErpPurchaseorderentryDto();
		List<ErpPurchaseorderentry> erpPurchaseorderentryList = BeanUtil.copyList(erpPurchaseorderentryDtoList, ErpPurchaseorderentry.class);
		String dataId = erpPurchaseorderService.addErpPurchaseorder(erpPurchaseorder, erpPurchaseorderentryList);
		if (dataId != null) {
			codeRuleService.useEncode("PurchaseOrder");
		}
		return dataId == null ? Response.notOk() : Response.ok(dataId);
	}

	/**
	 * 修改 
	 */
	@PutMapping("/{id}")
	@ApiOperation(value = "修改", notes = "传入erpPurchaseorder")
	public Response update(@PathVariable String id, @RequestBody SaveErpPurchaseorderFormDataDto formDto) {
		ErpPurchaseorderDto erpPurchaseorderDto = formDto.getErpPurchaseorderDto();
		ErpPurchaseorder erpPurchaseorder = BeanUtil.copy(erpPurchaseorderDto, ErpPurchaseorder.class);

		List<ErpPurchaseorderentryDto> erpPurchaseorderentryDtoList = formDto.getErpPurchaseorderentryDto();
		List<ErpPurchaseorderentry> erpPurchaseorderentryList = BeanUtil.copyList(erpPurchaseorderentryDtoList, ErpPurchaseorderentry.class);
		return Response.status(erpPurchaseorderService.updateErpPurchaseorder(id, erpPurchaseorder, erpPurchaseorderentryList));
	}


	
	/**
	 * 删除 
	 */
	@DeleteMapping("/{ids}")
	@ApiOperation(value = "删除", notes = "传入ids")
	public Response remove(@ApiParam(value = "主键集合", required = true) @PathVariable String ids) {
		return Response.status(erpPurchaseorderService.removeByIds(Func.toStrList(ids)));
	}

	
	/**
	 * excel批量导入数据
	 */
	@PostMapping("/import")
	@ApiOperation(value = "excel批量导入数据", notes = "传入菜单id")
	public Response importData(@RequestParam("F_ModuleId") String moduleId, MultipartFile file) {
		XjrExcelImport excelImport = importService.getByModuleId(moduleId);
		ImportDataParser parser = ParserFactory.getImportDataParser();
		File excelFile = IoUtil.toFile(file);
		List<Map<String, Object>> recordList = parser.parseDataToMap(excelImport, excelFile);
		if (CollectionUtil.isNotEmpty(recordList)) {
			List<ErpPurchaseorderDto> entityDtoList = recordList.stream().map(record -> {
				try {
					return objectMapper.readValue(objectMapper.writeValueAsString(record), ErpPurchaseorderDto.class);
				} catch (JsonProcessingException e) {
				}
				return null;
			}).collect(Collectors.toList());
			List<ErpPurchaseorder> entities = BeanUtil.copyList(entityDtoList, ErpPurchaseorder.class);
			return Response.status(erpPurchaseorderService.saveBatch(entities));
		}
		return Response.notOk();
	}
}
